<template>
  <div class="user-manage">
    <div class="title">
      <div class="block"></div>
      <div class="word">用户管理</div>
      <div class="button">
        <el-button class="el-button"
                   @click="refresh">刷新</el-button>
      </div>
    </div>
    <div>
      <search :formInline="formInline"
              @getSearchList="getSearchList">
        <div class="search-form">
          <el-form :inline="true"
                   :model="formInline"
                   class="demo-form-inline">
            <el-form-item class="el-form-item"
                          label="用户账号 :">
              <el-input class="el-input"
                        v-model.trim="formInline.username"
                        placeholder="用户名/用户账号"></el-input>
            </el-form-item>
            <el-form-item label="手机号 :">
              <el-input class="el-input"
                        v-model.trim="formInline.phone"
                        placeholder="手机号">
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </search>
    </div>
    <div>
      <datashow>
        <template v-slot:button>
          <el-button class="add"
                     @click="goUserAdd">添加</el-button>
        </template>
        <template v-slot:form>
          <el-table class="el-table"
                    ref="multipleTable"
                    :data="tableData"
                    border
                    style="width: 100%">
            <el-table-column prop="username"
                             label="用户账号"> </el-table-column>
            <el-table-column prop="nickname"
                             label="姓名"> </el-table-column>
            <el-table-column prop="phone"
                             label="手机号"> </el-table-column>
            <el-table-column prop="email"
                             label="邮箱"> </el-table-column>
            <el-table-column prop="description"
                             label="角色"> </el-table-column>
            <el-table-column prop="createTime"
                             label="添加时间">
            </el-table-column>
            <el-table-column label="是否启用">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.disabled"
                           :active-value="1"
                           :inactive-value="2"
                           active-color="#1abc9c"
                           inactive-color="#B9B9B9"
                           :disabled="scope.row.username == 'admin'?true:false"
                           @change="changeSwitch(scope.row)" />
              </template>
            </el-table-column>
            <el-table-column prop="operation"
                             label="操作">
              <template slot-scope="scope">
                <el-button @click="goEdit(scope.row)"
                           type="text"
                           size="small"
                           :disabled="scope.row.username == 'admin'&& isConsultant ? true : false">编辑</el-button>
                <el-button type="text"
                           size="small"
                           @click="deleteUser(scope.row)"
                           :disabled="scope.row.username == 'admin'?true:false">删除</el-button>
                <el-dialog title="提示"
                           :visible.sync="deleteDialogVisible"
                           width="30%"
                           center>
                  <div style="text-align:center">
                    <span>是否确实删除用户账号为{{deleteUserId}}的用户</span>
                  </div>
                  <span slot="footer"
                        class="dialog-footer">
                    <el-button @click="deleteDialogVisible = false">取 消</el-button>
                    <el-button type="primary"
                               @click="checkDeleteUser(scope.row)">确 定</el-button>
                  </span>
                </el-dialog>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </datashow>
    </div>
    <div class="paging">
      <paging :total="total"
              @size="size"
              @currentpage="currentpage"></paging>
    </div>
    <div>
      <footerinfo></footerinfo>
    </div>
  </div>
</template>

<script>
import search from "../../../component/search/search.vue";
import datashow from "../../../component/datashow/datashow.vue";
import paging from "../../../component/paging/paging.vue";
import footerinfo from "../../../component/footer/footer.vue";
import {
  getUserList,
  disableUser,//禁用/取消禁用单个用户
  deleteUser,//删除单个用户
} from "../../../api/index";

export default {
  inject: ['reload'],
  name: "userManage",
  components: {
    search, //筛选查询组件
    datashow, //数据列表组件
    paging, //分页组件
    footerinfo, //页脚组件
  },

  data () {
    return {
      current: '',
      pagesize: '',
      username: '',
      phone: '',
      deleteDialogVisible: false,
      deleteUserId: '',//删除用户账号为deleteUserId的用户
      isConsultant: false,
      total: 0,//一共多少条数据
      value1: true,
      value2: true,
      allSelect: false,
      formInline: {
        username: '', //用户名
        phone: '', //手机号
      },
      tableData: [],
    };
  },

  mounted () {
    this.$emit("routeName", "userManage");
    // console.log(window.sessionStorage.getItem("user"));
    if (window.sessionStorage.getItem("user") == "consultant") {
      this.isConsultant = true
    }
    this.getList()
  },

  methods: {
    refresh () {
      this.reload()
    },
    deleteUser (val) {
      this.deleteDialogVisible = true
      this.deleteUserId = val.username
    },
    checkDeleteUser(val) {
      this.deleteDialogVisible = false
      deleteUser(val.userId).then((data) => {
        console.log(data);
        this.getList()
      })
    },
    currentpage (val) {
      // console.log(val);
      this.current = val //得到新的当前页
      this.getList() //获取新的数据列表
    },
    size (val) {
      // console.log(val);
      this.pagesize = val //得到新当前页的一共几行数据
      this.getList() //获取新的数据列表
    },
    goUserAdd () {
      this.$router.push({
        name: "userAdd",
        params: {
          // userId: val.userId,
        },
      });
    },
    getList () {
      getUserList(this.current, this.pagesize, this.formInline.username, this.formInline.phone).then((data) => {
        this.tableData = data.data.records;
        data.data.records.forEach((item) => {
          if (item.disabled === false) {
            item.disabled = 1
          }
          if (item.disabled === true) {
            item.disabled = 2
          }
        })
        this.total = data.data.total
        console.log(data.data.records);
      });
    },
    toggleSelect (rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect);
        });
        this.allSelect = !this.allSelect;
      }
    },
    goEdit (val) {
      this.$router.push({
        name: "userEdit",
        params: {
          userId: val.userId,
        },
      });
    },
    changeSwitch (val) {
      disableUser(val.userId).then((data) => {
        console.log(data);
      })
    },
    getSearchList (val) { //点击查询获取新的数据列表
      this.current = '' //清空分页当前页数
      this.pagesize = ''//清空分页当前页的条数
      this.getList()
    }
  },
};
</script>

<style scoped>
.title {
  position: relative;
  display: flex;
  width: 100%;
  height: 50px;
  border-bottom: 1px solid #e4e4e4;
  line-height: 50px;
  background-color: #f3f3f3;
}
.block {
  width: 5px;
  height: 30px;
  margin-top: 10px;
  margin-left: 5px;
  background-color: #1abc9c;
}
/* 标题文字 */
.word {
  width: 100px;
  margin-left: 5px;
  color: #999999;
}
.button {
  position: absolute;
  right: 30px;
}
.search-form {
  width: 100%;
  padding-top: 10px;
  padding-left: 10px;
}
/* 筛选查询 */
.demo-form-inline {
  width: 1500px;
}
.el-switch {
  width: 50px;
  height: 30px;
}
.bottom {
  display: flex;
  text-align: center;
  margin: 10px 10px;
}
.el-checkbox {
  margin-top: 10px;
}
.el-button {
  margin-left: 20px;
}
.paging {
  margin-left: 300px;
}
.add {
  width: 100px;
  height: 40px;
  margin-top: 5px;
  margin-right: 20px;
}
.paging {
  text-align: center;
}
</style>
